package moe.loliserver.legacy.util.i18n;

import moe.loliserver.legacy.util.NumberUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.TimeZone;

public class i18n {
    public static ResourceBundle rb = ResourceBundle.getBundle("lang.message", new Locale(getLanguage(), getCountry()), new UTF8Control());

    public static String get(String key) {
        return rb.getString(key);
    }

    public static String get(String key, Object... f) {
        Object[] format;
        try {
            format = Arrays.stream(f).map(o1 -> NumberUtils.isNumber(String.valueOf(o1)) ? String.valueOf(new BigDecimal(Double.parseDouble(String.valueOf(o1)))) : String.valueOf(o1)).toArray();
        } catch (Throwable throwable) {
            format = new Object[] { throwable.toString() };
        }
        try {
            return new MessageFormat(get(key)).format(format);
        } catch (Exception e) {
            e.printStackTrace();
            return get(key);
        }
    }

    //TODO This Method is Deleted in LoliServer1.17
    public static String getLocale(int key) {
        return "xx_XX";
    }

    public static String getLanguage() {
        return getLocale(1);
    }

    public static String getCountry() {
        return getLocale(2);
    }

    public static String getLocale() {
        return rb.getLocale().toString();
    }

    public static boolean isCN() {
        TimeZone timeZone = TimeZone.getDefault();
        return timeZone.getID().equals("Asia/Shanghai") || getLocale().contains("CN") || getCountry().contains("CN");
    }
}